package Utils;

import Actions.*;

import com.alibaba.fastjson.JSONObject;

import io.restassured.response.Response;
import org.testng.Assert;

import java.util.HashMap;
import java.util.Map;

import static io.restassured.RestAssured.given;

public class BasicTest {
    public SMSActions sms=new SMSActions();
    public BaseAction mms=new BaseAction();
    public WMSActions wms=new WMSActions();
    public IMSAction ims=new IMSAction();
    public BaseAction base =new BaseAction();
    public Log log=new Log(this.getClass());
    public static String URL="http://hc-t1.yonghuivip.com/app/api/";
    public static Map<String,String> cookies=new HashMap<String, String>();
    public Response response;
    public String current_Date =Utils.getCurrentDate();

    public String resString;
    public JSONObject json;

    public static   String VH9D15="170952316587872256";
    public static  String VHW078="156823763197906944";
    public static  String VH9D13="162249205597896704";
    public String pono;
    public String tfno;
    public void loginSMS(){
        sms.loginSMS();
    }
    public void loginSMS(String username,String pwd){
        loginSMS(username,pwd);
    }
    //获取采购单信息
    public Response getPOInfo(String pono){
        return sms.getPOInfo(pono);
    }
    //获取采购单状态
    public String getPOStatus(Response res){
        return sms.getPOStatus(res);
    }

    //获取转移单信息
    public Response getTFInfo(String tfno){
        return sms.getTFInfo(tfno);
    }

    //获取转移单状态
    public String getTFStatus(Response res){
        return sms.getTFStatus(res);
    }

    //获取需求单信息
    public Response getPRInfo(String shoptype,String billno){

        return sms.getPRInfo(shoptype,billno);

    }

    //获取需求单状态
    public String getPRStatus(Response res){
        System.out.println(res.asString());
        String status=res.jsonPath().getString("data.rows[0].statusname");
        return status;
    }

    //获取出库单信息
    public Response getCKInfo(String no){
        return wms.getCKInfo(no);


    }

    public Response fnishCK(String billno) {
        response=wms.createWave(billno);
        return response;
    }

    public Response getSHInfo(String vhid, String no){
        return wms.getSHInfo(vhid,no);
    }

    public String getSHStatus(Response res){
        return wms.getSHStatus(res);
    }



    //查询商品凭证
    public Response getGoodsReceipt(String shopid,String billno){
        int fail=10;
        response= ims.getGoodsReceipt(shopid,billno);
        String id=null;
        response= ims.getGoodsReceipt(shopid,billno);

        id=response.jsonPath().getString("data.rows[0].id");
        while(null==id&&fail>0){
            fail--;
            log.info("没有查询到单据"+billno+"商品凭证,10秒后重新查询......");
            base.waitSecond(10);
            response= ims.getGoodsReceipt(shopid,billno);
            id=response.jsonPath().getString("data.rows[0].id");
        }

            Assert.assertNotEquals(null,id,"没有查询到"+billno+"商品凭证！");
            String articleno=response.jsonPath().getString("data.rows[0].articleno");
            log.info(billno+"凭证ID为:"+id+"凭证编号为:"+articleno);

        return response;
    }



    //创建采购单
    public Response createPO(String type,String shopcode,String skucode,Integer number){
        response= sms.createPO(type,shopcode,skucode,number);
        pono=response.asString();
        return  response;
}

    //创建需求单
    public String createPR(String shopcode,String skucode,Integer skunumber){
        response=sms.createPR(shopcode,skucode,skunumber);
        String prNO=response.jsonPath().getString("data[0]");
        log.info("新建需求单成功！需求单号:"+prNO);
        return prNO;
    }

    //创建转移单
    public String createTF(String shopin,String shopout,String skucode,Integer skunumber){
        response=sms.createTF(shopout,shopin,skucode,skunumber);

        return response.asString();
    }

    //审批单据 转移单、采购单
    public Response approveBills(String billno){
        return  sms.approveBills(billno);
    }
    //审批需求单
    public Response approvePR(String billno){
        response= sms.approvePR(billno);
        String status=response.jsonPath().getString("data.rows[0].statusname");
        Assert.assertNotEquals(status,"已提交");
        log.info("需求单:"+billno+"审批成功！");
        if(sms.logisticsmode==1){
            pono=null;
            pono=response.jsonPath().getString("data.rows[0].purchid");
            log.info("关联采购单单号为："+pono);
        }else if(sms.logisticsmode==2){
            tfno=null;
            tfno=response.jsonPath().getString("data.rows[0].transid");
            log.info("关联转移单号为："+tfno);
        }else{
            pono=null;
            tfno=null;
            pono=response.jsonPath().getString("data.rows[0].purchid");
            tfno=response.jsonPath().getString("data.rows[0].transid");
            log.info("关联采购单单号为："+pono+"转移单号为："+tfno);
        }
        return response;

    }
    //获取单据信息
    public Response getBillInfo(String billno){
       return sms.getBillInfo(billno);
    }

    //获取单据状态
    public String getBillStatus(Response res){
       return sms.getBillStatus(res);
    }

    //WMS入库操作
    public Response wmsReceive(String shopcode,String billno){
        response=wms.wmsReceive(shopcode,billno);

        return response;
    }

    public Response quickDelivery(String billno){
          return  wms.quickDelivery(billno);
    }

    public Response closeSHBill(String warehouseid, String receiptid) {
        return wms.closeSHBill(warehouseid,receiptid);
    }

    public Response receiveSKU(String warehouseid, String receiptdetailid, String skunumber) {
       return wms.receiveSKU(warehouseid,receiptdetailid,skunumber);
    }

    public Response enterSHBill(String warehouseid, String receiptid) {
        return wms.enterSHBill(warehouseid,receiptid);
    }

    public String getWarehouseID(String shopcode) {
        return  wms.getWarehouseID(shopcode);
    }

    public Response findRelationship(String shopcode,String skucode){
        return sms.findRelationship(shopcode,skucode);
    }
    public Response findskushop(String shopcode,String skucode){
        return sms.findskushop(shopcode,skucode);
    }



}
